import Tpl from './index.ejs';
import ListTpl from './list.ejs';

import Tools from '../tools/tools.js';

class Order {

	init ($wrap) {

		$wrap.html(Tpl());

		this.$checkbox = $('.ui.checkbox');
		this.$checkBtn = $('#checkBtn');
		this.$listWrap = $('#listWrap');

		this.initDate();
		this.bindEvent();
	}

	bindEvent () {
		let self = this;

		this.$checkbox.checkbox();

		this.$checkBtn.on('click', function () {
			self.submitForm();
		});
	}

	submitForm () {
		let self = this;
        let reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
        }, Tools.getFromData());

        $.ajax({
			url: '/order/settle/queryPage',
			method: 'post',
			data: JSON.stringify(reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(ListTpl(data));
				}
			}
        });
	}

	initDate () {

		$( "#from" ).datetimepicker({
	    	defaultDate: "+1w",
	    	changeMonth: true,
	    	numberOfMonths: 1,
			dateFormat: "yy-mm-dd",
            showSecond: true,
            timeFormat: 'hh:mm:ss',
            stepHour: 1,
            stepMinute: 1,
            stepSecond: 1,
	    	onClose: function( selectedDate ) {
	     		$( "#to" ).datetimepicker( "option", "minDate", selectedDate );
	    	}
	    });
	    $( "#to" ).datetimepicker({
	    	defaultDate: "+1w",
	     	changeMonth: true,
	    	numberOfMonths: 1,
			dateFormat: "yy-mm-dd",
            showSecond: true,
            timeFormat: 'hh:mm:ss',
            stepHour: 1,
            stepMinute: 1,
            stepSecond: 1,
	    	onClose: function( selectedDate ) {
	    		$( "#from" ).datetimepicker( "option", "maxDate", selectedDate );
	    	}
	    });
	}
}

export default new Order();
